/**
 * @author Gabriel Tuma, Sebastina Demler
 * 
 * The MainActivity ans View Class.
 */

package com.android.datecalculator;

import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;

public class Datecalculator extends Activity implements Observer{
	
	private Model model;
	private AlertDialog about_us_dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_datecalculator);
        
        // The Model
        model 	= new Model(this);
        model.addObserver(this);		// Adds the specified observer to the list of observers.
        
        // Start-Datepicker + onDateChangedListener
        DatePicker dpStart = (DatePicker) findViewById(R.id.datePickerStart);
        dpStart.init(dpStart.getYear(), dpStart.getMonth(), dpStart.getDayOfMonth(), new OnDateChangedListener() {
			
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				model.setStart(year, monthOfYear, dayOfMonth);
			}
		});
        
        // End-Datepicker + onDateChangedListener
        DatePicker dpEnd = (DatePicker) findViewById(R.id.datePickerEnd);
        dpEnd.init(dpEnd.getYear(), dpEnd.getMonth(), dpEnd.getDayOfMonth(), new OnDateChangedListener() {
        	
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				model.setEnd(year, monthOfYear, dayOfMonth);
			}
		});
        
        // Set Start + End in the Model
        model.setStart(dpStart.getYear(), dpStart.getMonth(), dpStart.getDayOfMonth());
        model.setEnd(dpEnd.getYear(), dpEnd.getMonth(), dpEnd.getDayOfMonth());
        
        __creatAboutUsDialog();
        
        if(model.newinstalled())
        	about_us_dialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_datecalculator, menu);
        model.setTitleMenuItemSwitchDU(menu.getItem(0));
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()){
    		case R.id.menu_switch:
    			model.switchDifferenceUnit(item);
    			break;
    		case R.id.menu_about:
    			about_us_dialog.show();
    			break;
    	}
    	return true;
    }
    
    
    
    /* 
	 * ***********************************************************************
	 * 							Private Methodes
	 * ***********************************************************************
	 */
    
    /**
     * implements the java.util.Observer.update Method
     * 
     * This method is called if the specified Observable object's notifyObservers method is called <br> 
     * (because the Observable object has been updated
     */
	public void update(Observable arg0, Object arg1) {
		((TextView) findViewById(R.id.diff)).setText(
				getString(R.string.diff) + " " + 
				getString( (model.getOptions().getDifference_unit() == DifferenceUnits.HOURS) ? R.string.diff_unit_hours :  R.string.diff_unit_day ) + ": " + 
				model.calcDiff()
				);
	}
	
	/**
	 * Creat the Dialog. Otherweise is the Dialog null
	 */
	private void __creatAboutUsDialog(){
		about_us_dialog = new AlertDialog.Builder(this)
		.setView(getLayoutInflater().inflate(R.layout.aboutdialog, null))
		.create();
	}
	
	
	
	/* 
	 * ***********************************************************************
	 * 							Private Methodes
	 * ***********************************************************************
	 */
	
	/**
	 * This Methode is invoked in the aboutdialog.xml 
	 */
	public void cancel_about_us_dialog(View v){
		about_us_dialog.cancel();
	}
}
